﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;

namespace Shine.Util.Data
{
    /// <summary>
    /// 概述：System.Data 扩展类<br />
    /// </summary>
    public static class DataExtension
    {        
        /// <summary>
        /// 将DataTable转化成泛型T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="datatable"></param>
        /// <returns></returns>
        public static List<T> ToObject<T>(this System.Data.DataTable datatable)
        {
            return DataTableHelper.ToObject<T>(datatable);
        }

        /// <summary>
        /// 将数据行DataRow转成键值对,键都为大写。
        /// </summary>
        /// <param name="dataRow">数据行</param>
        /// <returns></returns>
        public static Dictionary<string, object> ToDictionary(this System.Data.DataRow dataRow)
        {
            Dictionary<string, object> dr = new Dictionary<string, object>();
            foreach (System.Data.DataColumn dataColumn in dataRow.Table.Columns)
            {
                dr.Add(dataColumn.ColumnName.ToUpper(), (dataRow[dataColumn] == DBNull.Value ? null : dataRow[dataColumn]));
            }
            return dr;
        }
    }
}
